package com.jinzhi.jzweb.controller.resourceLibrary;



import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.eventresource.main.EventresourceAllocationDO;
import com.jinzhi.jzweb.domain.resourceLibrary.SecretKey;
import com.jinzhi.jzweb.pojo.RegionItemVO;
import com.jinzhi.jzweb.service.resourceLibrary.SecretKeyService;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 
 * <pre>
 * 赛道抽签表（存储抽签信息）
 * </pre>
 * <small> 2024-02-28 09:32:18 | lance</small>
 */
@Controller
@RequestMapping("/secretKey")
@Api(value = "秘钥管理", tags = {"秘钥管理"})
public class SecretKeyController extends BaseController {


	/**
	 * 服务对象
	 */

	private final SecretKeyService secretKeyService;

	public SecretKeyController(SecretKeyService secretKeyService) {
		this.secretKeyService = secretKeyService;
	}

	@ApiOperation(value = "密钥管理页面地址", notes = "密钥管理页面地址")
	@GetMapping("keyManagement")
	@RequiresPermissions("jzweb:secretKey:keyManagement")
	String keyManagement(){
		return "jzweb/keyManagement/keyManagement";
	}


	@ApiOperation(value = "密钥管理新增页面地址", notes = "密钥管理新增页面地址")
	@GetMapping("add/")
	@RequiresPermissions("jzweb:secretKey:add")
	String add(Model model){
		Map<String, List<EventresourceAllocationDO>> map  = secretKeyService.getAddSecretKy();

		Map<String, List<EventresourceAllocationDO>> newMap = new HashMap<>();
		for (String s : map.keySet()) {
			if (s.equals("1+X证书资源")) {
				newMap.put("证书资源", map.get(s));
			}else {
				newMap.put(s, map.get(s));
			}
		}
		model.addAttribute("map", newMap);
		return "jzweb/keyManagement/add";
	}


	@ApiOperation(value = "密钥管理编辑页面地址", notes = "密钥管理编辑页面地址")
	@GetMapping("edit/{id}")
	@RequiresPermissions("jzweb:secretKey:edit")
	String edit(Model model,@PathVariable("id")Long id){
		SecretKey secretKey = secretKeyService.getSecretKy(id);
		model.addAttribute("secretKey", secretKey);
		return "jzweb/keyManagement/edit";
	}



	/**
	 * 新增数据
	 *
	 * @param secretKey 实体对象
	 * @return 新增结果
	 */
	@ApiOperation(value = "新增秘钥", notes = "新增秘钥")
	@ResponseBody
	@PostMapping("/saveSecretKey")
	public Result<Object> saveSecretKey(@RequestBody SecretKey secretKey) {
		return this.secretKeyService.saveSecretKey(secretKey);
	}

	/**
	 * 修改数据
	 *
	 * @param secretKey 实体对象
	 * @return 修改结果
	 */
	@ApiOperation(value = "修改秘钥", notes = "修改秘钥")
	@ResponseBody
	@PostMapping("/editSecretKey")
	public Result<Object> editSecretKey(@RequestBody SecretKey secretKey) {
		return this.secretKeyService.editSecretKey(secretKey);
	}

	/**
	 * 删除数据
	 *
	 * @param id 结合
	 * @return 删除结果
	 */
	@ApiOperation(value = "删除秘钥", notes = "删除秘钥")
	@ResponseBody
	@GetMapping(value = "/deletedById")
	public Result<Object> deletedById(Long id) {
		return this.secretKeyService.removeById(id);
	}


	@ApiOperation(value = "多条件列表分页查询", notes = "多条件列表分页查询")
	@ApiResponses({
			@ApiResponse(code = 0, message = "操作成功",response = SecretKey.class),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@ResponseBody
	@PostMapping(value = "/getSecretKyPage")
	public Result<Object> getSecretKyPage(@RequestBody SecretKey secretKey){
		return this.secretKeyService.getSecretKyPage(secretKey);
	}

	@ApiOperation(value = "定时任务测试", notes = "定时任务测试")
	@ApiResponses({
			@ApiResponse(code = 0, message = "操作成功",response = SecretKey.class),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@ResponseBody
	@PostMapping(value = "/timingEffect")
	public Result<Object> timingEffect(){
		this.secretKeyService.timingEffect();
		return Result.ok();
	}


	@ApiOperation(value = "根据资源id，用户id验证是否有查看资源权限", notes = "根据资源id，用户id验证是否有查看资源权限")
	@ApiResponses({
			@ApiResponse(code = 0, message = "操作成功",response = SecretKey.class),
			@ApiResponse(code = 500, message = "服务器内部异常")})
	@ResponseBody
	@PostMapping(value = "/permissionVerification")
	public Result<Object> permissionVerification(Long userId,Long allocationId){
		return Result.ok(this.secretKeyService.permissionVerification(userId,allocationId));
	}

	/**
	 * 获取省市区的树形结构数据
	 * @return
	 */
	@GetMapping("/getAreaTree")
	@ResponseBody
	public Result getAreaTree() {
		List<RegionItemVO> areaItemVoList = secretKeyService.getRegionTree();
		return Result.ok(areaItemVoList);
	}

	@GetMapping("/permission")
//    @RequiresPermissions("jzweb:permission:permission")
	String permissionAdd(String id, Model model) {
		Map<String, List<EventresourceAllocationDO>> map  = secretKeyService.getAddSecretKy();

		Map<String, List<EventresourceAllocationDO>> newMap = new HashMap<>();
		for (String s : map.keySet()) {
			if (s.equals("1+X证书资源")) {
				newMap.put("证书资源", map.get(s));
			}else {
				newMap.put(s, map.get(s));
			}
		}
		model.addAttribute("map", newMap);
		return "jzweb/keyManagement/permission";
	}
}
